package org.virtualdb.socket.proxy;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class JSockProxyInitializer extends ChannelInitializer<SocketChannel> {

    private final String remoteHost;
    private final int remotePort;

    public JSockProxyInitializer(String remoteHost, int remotePort) {
        this.remoteHost = remoteHost;
        this.remotePort = remotePort;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        Object log = System.getProperty("do_log");
        if ("true".equals(log)) {
            ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
        }
        ch.pipeline().addLast(new JSockProxyFrontendHandler(remoteHost, remotePort));
    }
}
